package com.fer.tankr;

import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.os.Bundle;
import android.os.Vibrator;

import com.fer.andenginemvc.api.Controller;
import com.fer.tankr.controllers.GameController;
import com.fer.tankr.scenes.SceneManagerImpl;
import com.parse.Parse;

public class MainActivity extends SimpleBaseGameActivity{

	public final static float CAMERA_WIDTH = 800f;
	public final static float CAMERA_HEIGHT = 480f;
	
	public AssetManagerImpl assetManager;
	public SceneManagerImpl sceneManager;	
	public Camera camera;
	
	public Stack<String> sceneStack;
	public Controller<MainActivity> currentController;
	
	@Override
	protected void onCreate(Bundle pSavedInstanceState) {
		super.onCreate(pSavedInstanceState);
		Parse.initialize(this, "VswWNvRGoQVvpUf2Bqh7w8abB9ymJtoh1v2o2BUa", "wMPGvAHzCIDr5PNRIOC7zd4QcSxcK3R91vjpyqHE"); 
		
		GlobalState.get().init(this);
	}
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions opts =  new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new FillResolutionPolicy(), camera);
		opts.getTouchOptions().setNeedsMultiTouch(true);
		return opts;
	}
	
	

	@Override
	protected void onCreateResources() {
		
		assetManager = new AssetManagerImpl(this);
		assetManager.onLoad();
		
	}

	@Override
	protected Scene onCreateScene() {
		mEngine.registerUpdateHandler(new FPSLogger());

		sceneManager = new SceneManagerImpl(this);
		sceneStack = new Stack<String>();
		
		return sceneManager.get(SceneManagerImpl.SPLASH).getScene();
	}

	public void vibrate()
	{
		Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		v.vibrate(100);
	}
	
	public void setScene(String name)
	{
		sceneStack.add(name);
		for (String s : sceneStack) {
			System.out.print(s+",");
		}
		System.out.println();
		currentController = sceneManager.get(name);
		mEngine.setScene(currentController.getScene());
	}
	
	@Override
	public void onBackPressed() {
		String currentScene = sceneStack.pop();
		if (currentScene.equals(SceneManagerImpl.MENU))
		{
			finish();
		}
		if (currentScene.equals(SceneManagerImpl.GAME))
		{
			sceneStack.add(currentScene);
			
			GameController controller = (GameController)currentController;
			if (controller.isPaused())
			{
				controller.resume();
			}
			else
			{
				controller.pause();
			}
		}
		else
		{
			String scene = null;
			try {
				scene = sceneStack.pop();
				if (scene!=null)
				{
					setScene(scene);
				}
				else
				{
					finish();
				}
			} catch (Exception e) {
				finish();
			}
			
		}
		
	}


}
